package com.jiji.miaosha.service.impl;

import com.jiji.miaosha.entity.User;
import com.jiji.miaosha.entity.UserPassword;
import com.jiji.miaosha.error.BusinessException;
import com.jiji.miaosha.error.EmBusinessError;
import com.jiji.miaosha.mapper.UserMapper;
import com.jiji.miaosha.mapper.UserPasswordMapper;
import com.jiji.miaosha.service.UserService;
import com.jiji.miaosha.service.model.UserModel;
import com.jiji.miaosha.validator.ValidationResult;
import com.jiji.miaosha.validator.ValidatorImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private UserPasswordMapper userPasswordMapper;

    @Autowired
    private ValidatorImpl validator;

    @Override
    public UserModel getUserById(Integer id) {
        User user = userMapper.queryUserById(id);
        if (user == null) {
            throw new BusinessException(EmBusinessError.USER_NOT_EXIST);
        }
        UserPassword userPassword = userPasswordMapper.queryPasswordByUserId(id);
        return convertFromEntity(user, userPassword);
    }

    @Override
    @Transactional
    public void register(UserModel userModel) {
        if (userModel == null) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR);
        }
//        if (StringUtils.isEmpty(userModel.getName())
//                || userModel.getGender() == null
//                || userModel.getAge() == null
//                || StringUtils.isEmpty(userModel.getTelephone())) {
//            throw new UserException(UserEnum.PARAMETER_VALIDATION_ERROR);
//        }
        ValidationResult result = validator.validate(userModel);
        if (result.isHasErrors()) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,result.getErrMsg());
        }
        User user = convertFromModel(userModel);
        try {
            userMapper.upSert(user);
        } catch (DuplicateKeyException e) {
            throw new BusinessException(EmBusinessError.PARAMETER_VALIDATION_ERROR,"手机号已重复注册");
        }
        UserPassword userPassword = convertPasswordFromModel(userModel);
        userPasswordMapper.upSert(userPassword);
    }

    @Override
    public UserModel validateLogin(String telephone, String encrptPassword) {
        User user = userMapper.queryUserByTelephone(telephone);
        if (user == null) {
            throw new BusinessException(EmBusinessError.USER_LOGIN_FAIL);
        }
        UserPassword userPassword = userPasswordMapper.queryPasswordByUserId(user.getId());
        UserModel userModel = convertFromEntity(user, userPassword);
        if (!StringUtils.equals(encrptPassword, userModel.getEncrptPassword())) {
            throw new BusinessException(EmBusinessError.USER_LOGIN_FAIL);
        }
        return userModel;
    }

    private UserPassword convertPasswordFromModel(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        UserPassword userPassword = new UserPassword();
        userPassword.setEncrptPassword(userModel.getEncrptPassword());
        userPassword.setUserId(userModel.getId());
        return userPassword;
    }

    private User convertFromModel(UserModel userModel) {
        if (userModel == null) {
            return null;
        }
        User user = new User();
        BeanUtils.copyProperties(userModel, user);
        return user;
    }

    private UserModel convertFromEntity(User user, UserPassword userPassword) {
        UserModel userModel = new UserModel();
        BeanUtils.copyProperties(user, userModel);
        if (userPassword != null) {
            userModel.setEncrptPassword(userPassword.getEncrptPassword());
        }
        return userModel;
    }
}
